/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 * Checkbox widget
 * <p>
 * <img src="../../../resources/aphro_cb.png">
 * </p>
 * 
 * 
 * @author kentelt
 * 
 */
public final class CheckBox extends TraversableWidget {
	private int boxx;

	private int boxy;
	private boolean isSelected;
	private String text;

	/**
	 * @param theme
	 *            to be used by the widget
	 * @param text
	 *            Checkbox text
	 * @throws NullPointerException
	 *             if text is null
	 */
	public CheckBox(Theme theme, String text) {
		super(theme, 0, 0);
		if (text == null) {
			throw new NullPointerException();
		}
		this.text = text;
		updateModel();
	}

	public void cleanUp() {
		text = null;

	}

	public void fire() {
		isSelected = !isSelected;
		reDrawWidget();
		super.fire();
	}

	public boolean pointerPress(int px, int py) {
		if (super.pointerPress(px, py)) {
			fire();
			return true;
		}
		return false;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, true);
		WidgetHelper.drawCheck(g, boxx, boxy, isSelected);
		g.setColor(theme.fgColor);
		if (theme.bmFont != null) {
			theme.bmFont.drawString(g, text, 2 + 15 + 2, 2, 0);
		} else {
			g.setFont(theme.widgetFont);
			g.drawString(text, 2 + 15 + 2, 2, 0);
		}

		release(g, width, height, true);

	}

	public void setSelected(boolean selected) {
		this.isSelected = selected;
		reDrawWidget();
	}

	public void setText(String text) {
		this.text = text;
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	protected final boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		if (dir == Canvas.DOWN || dir == Canvas.RIGHT) {
			if (location == UPPER) {
				location = IN;
			} else {
				return false;
			}
		} else if (dir == Canvas.UP || dir == Canvas.LEFT)
			if (location == LOWER) {
				location = IN;
			} else {
				return false;
			}
		return true;
	}

	protected void updateModel() {
		int boxHt = 0;
		if (theme.bmFont != null) {
			boxHt = theme.bmFont.getHeight();
			width = 2 + WidgetHelper.checkBoxHeight + 2 + theme.bmFont.stringWidth(text) + 2;
		} else {
			boxHt = theme.widgetFont.getHeight();

			// cb left pad+ cb width +2+ text width+cb right pad
			width = 2 + WidgetHelper.checkBoxHeight + 2 + theme.widgetFont.stringWidth(text) + 2;
		}
		// 15+ toppad+bottom pad if less than 15
		height = (boxHt < WidgetHelper.checkBoxHeight) ? WidgetHelper.checkBoxHeight + 4 : boxHt + 4;
		boxx = 2;
		boxy = ((height - WidgetHelper.checkBoxHeight) / 2);

	}

}
